package edu.pku.sei.modeler.neditor.command;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.notation.core.NotationFactory;
import edu.pku.sei.modeler.model.notation.core.NotationPackage;
import edu.pku.sei.modeler.model.notation.shape.ConnectionTemplate;
import edu.pku.sei.modeler.neditor.model.Diagram;

public class CreateLineCommand extends Command {
	
	private MetaModelElement parent = null;
	private String typeID = null;
	private NotationFactory factory = null;
	
	private Point location;
	
	public void setFactory(NotationFactory factory) {
		this.factory = factory;
	}
	public void setLocation(Point location) {
		if(location==null) return;
		this.location=location;
	}
	public void setParent(MetaModelElement parent) {
		this.parent = parent;
	}
	public void setTypeID(String typeID) {
		this.typeID = typeID;
	}

	@Override
	public boolean canExecute() {
		if(typeID!=null&&typeID==NotationPackage.TYPE_CONNECTION)
			return true;
		return false;
	}
	
	private ConnectionTemplate node = null;
	@Override
	public void execute() {
		if(parent instanceof Diagram) {
			node = (ConnectionTemplate)factory.createElement(typeID, true);
			node.setLocation(location.x, location.y);
			node.setStartPoint(location.x,location.y);
			node.setEndPoint(location.x+100, location.y);
			node.setModel(parent.getModel());
			
			((Diagram)parent).addLinks(node);
			
		} 
	}

	@Override
	public void redo() {
		// TODO Auto-generated method stub
		//super.redo();
		if(parent instanceof Diagram) {
			factory.registerToPackage(node, typeID);
			((Diagram)parent).addLinks(node);
		}
	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub
		if (parent instanceof Diagram) {
			((Diagram)parent).removeLinkes(node);
			factory.unRegisterFromPackage(node);
		}
	}

}
